﻿/*========================== 
 * @author 郑金泉
 * @desc 数据库表字段类
 * ========================= */
using System;
using System.Collections.Generic;
using System.Text;

namespace Obsidian.Data.Sql
{
    public class DbField
    {

        private DbTable _dbTable;
        /// <summary>
        /// 数据库表
        /// </summary>
        public DbTable Table
        {
            get { return this._dbTable; }
        }

        private string _columnName;
        /// <summary>
        /// 字段名称
        /// </summary>
        public string Name
        {
            get { return this._columnName; }
        }

        private bool _isPrimaryKey;
        /// <summary>
        /// 是否是主键
        /// </summary>
        public bool IsPrimaryKey
        {
            get { return this._isPrimaryKey; }
        }

        private bool _allowNull;
        /// <summary>
        /// 是否允许为空
        /// </summary>
        private bool AllowNull
        {
            get { return this._allowNull; }
        }

        private bool _isAutoIncrement;
        /// <summary>
        /// 是否是自动增长列
        /// </summary>
        public bool IsAutoIncrement
        {
            get { return this._isAutoIncrement; }
        }

        private DataType _dataType;
        /// <summary>
        /// 数据类型
        /// </summary>
        public DataType DataType
        {
            get { return this._dataType; }
        }

        private string _tablePrefixName = null;
        /// <summary>
        /// 表前缀名称
        /// </summary>
        public string TablePrefixName
        {
            get 
            {
                if (this._tablePrefixName == null)
                {
                    switch (this.Table.DataBase.Type)
                    {
                        case DataBaseType.SqlServer:
                        case DataBaseType.OleDb:
                            this._tablePrefixName = "[" + this._dbTable.Name + "].[" + this._columnName + "]";
                            break;
                        case DataBaseType.MySql:
                            this._tablePrefixName = "`" + this._dbTable.Name + "`.`" + this._columnName + "`";
                            break;
                        default:
                            throw new Exception("未知数据库类型");
                    }
                }
                return this._tablePrefixName;
            }
        }

        private string _queryAlias = null;
        /// <summary>
        /// 查询别名
        /// </summary>
        public string QueryAlias
        {
            get
            {
                if (this._queryAlias == null)
                    this._queryAlias = this._dbTable.Name + "_" + this._columnName;
                return this._queryAlias;
            }
        }

        public DbField(DbTable dbTable, string columnName, bool isPrimaryKey, bool allowNull, bool isAutoIncrement, DataType dataType)
        {
            this._dbTable = dbTable;
            this._columnName = columnName;
            this._isPrimaryKey = isPrimaryKey;
            this._allowNull = allowNull;
            this._isAutoIncrement = isAutoIncrement;
            this._dataType = dataType;
        }

    }

    public enum DataType
    {
        Unknown,
        Bit,
        Bool,
        Int,
        Bigint,
        Decimal,
        Float,
        Char,
        VarChar,
        Text,
        DateTime
    }

}
